package com.iraeta.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.entity.main.Srmmessage;
import com.iraeta.entity.main.type.SrmMessageStatus;
import com.iraeta.service.ProcessMessageTask;
import com.iraeta.service.basic.SrmmessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "SrmMsgController")
@RequestMapping("srmmsg")
public class SrmMsgController extends BaseController{
    @Autowired
    SrmmessageService srmmessageService;
    @Autowired
    ProcessMessageTask processMessageTask;

    @RequestMapping("listsrmmessage")
    public String listSrmmessage(@RequestBody JSONObject jo){
        String typeName = jo.getString("typeName");
        String orderData = jo.getString("orderData");
        String startDate = jo.getString("startDate");
        String endDate = jo.getString("endDate");
        this.pageSet(jo);
        List<Srmmessage> srmmessageList = srmmessageService.listByCondition(typeName, orderData, startDate, endDate);
        srmmessageList.forEach(it->it.setStatusname(SrmMessageStatus.getSrmMessageStatusStr(it.getStatusid())));
        PageInfo<Srmmessage> srmmessagePageInfo = this.getPageInfo(srmmessageList);
        return this.success(srmmessagePageInfo);
    }

    @RequestMapping("listerrorsrmmessage")
    public String listErrorSrmmessage(@RequestBody JSONObject jo){
        String typeName = jo.getString("typeName");
        String orderData = jo.getString("orderData");
        String startDate = jo.getString("startDate");
        String endDate = jo.getString("endDate");
        this.pageSet(jo);
        List<Srmmessage> srmmessageList = srmmessageService.listErrorMsg(typeName, orderData, startDate, endDate);
        PageInfo<Srmmessage> srmmessagePageInfo = this.getPageInfo(srmmessageList);
        return this.success(srmmessagePageInfo);
    }

    @RequestMapping("processerrorsrmmessage")
    public String processErrorSrmmessage(@RequestBody JSONObject jo){
        Long id = jo.getLongValue("id");
        Srmmessage srmmessage = srmmessageService.getById(id);
        processMessageTask.processSrmMessageOne(srmmessage);
        if(StrUtil.isEmpty(srmmessage.getErrormsg())){
            return this.success();
        }else{
            return JSON.toJSONString(Result.error(srmmessage.getErrormsg()));
        }
    }
}
